#include "QtRoborace.h"


#include <QKeyEvent>

QtRoborace::QtRoborace(QWidget *parent, Qt::WFlags flags): QMainWindow(parent, flags)
{
	ui.setupUi(this);

	world = new QtWorld();

	QObject::connect(world, SIGNAL(LogFinished()), this, SLOT(OnPlayFinished()));

	world->Resize(6, 6);

	Wall* wall1 = world->CreateWall(Direction::NORTH, 0, 1);
	robot1 = world->CreateRobot("red", 0, 2);
	robot2 = world->CreateRobot("green", 0, 3);
	robot3 = world->CreateRobot("blue", 0, 4);
	pusher1 = world->CreatePusher(Direction::SOUTH, 2, 4);
	laser1 = world->CreateLaser(Direction::SOUTH, 3, 4);
	spinner1 = world->CreateSpinner(Direction::EAST, 4,4);
	spinner2 = world->CreateSpinner(Direction::WEST, 3,2);

	world->AddObject(robot1);
	world->AddObject(robot2);
	world->AddObject(robot3);
	world->AddObject(wall1);
	world->AddObject(pusher1);
	world->AddObject(laser1);
	world->AddObject(spinner1);
	world->AddObject(spinner2);

	treadmillSprite = new QtTreadmillSprite();
	treadmillSprite->AddToScene(world);
	treadmillSprite->SetPosition(QPoint(-150, 0));
	treadmillSprite->SetDirection(Direction::EAST);
	treadmillSprite->SetPixmap( tempTilemanager.Tile(*treadmillSprite, "foreground") );

	checkpointSprite = new QtCheckpointSprite();
	checkpointSprite->AddToScene(world);
	checkpointSprite->SetPosition(QPoint(-150, 64));
	checkpointSprite->SetPixmap( tempTilemanager.Tile(*checkpointSprite, "foreground") );

	repairpointSprite = new QtRepairpointSprite();
	repairpointSprite->AddToScene(world);
	repairpointSprite->SetPosition(QPoint(-150, 64*2));
	repairpointSprite->SetPixmap( tempTilemanager.Tile(*repairpointSprite, "foreground") );

	pitfallSprite = new QtPitfallSprite();
	pitfallSprite->AddToScene(world);
	pitfallSprite->SetPosition(QPoint(-150, 64*3));
	pitfallSprite->SetPixmap( tempTilemanager.Tile(*pitfallSprite, "foreground") );


	ui.graphicsView->setScene(world);
	ui.graphicsView->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

	robot = robot1;
	canReadKeys = true;
}

QtRoborace::~QtRoborace()
{

}

void QtRoborace::OnPlayFinished()
{
	canReadKeys = true;
}

void QtRoborace::NextRound()
{
	// program all cards

	// do 5 simulations

	// schedule laser shoot for all robots

	// do simulation

	// play logbook

	world->Simulate();

	printf("Play\n");
	canReadKeys = false;
	world->Play();
	
}

void QtRoborace::keyPressEvent(QKeyEvent *e)
{
	if (!canReadKeys)
		return;

	switch(e->key())
	{
		case Qt::Key_1:
			robot = robot1;
		break;
		case Qt::Key_2:
			robot = robot2;
		break;
		case Qt::Key_3:
			robot = robot3;
		break;

		case Qt::Key_Z:
			robot->ScheduleMoveUp();
		break;
		case Qt::Key_Q:
			robot->ScheduleMoveLeft();
		break;
		case Qt::Key_S:
			robot->ScheduleMoveDown();
		break;
		case Qt::Key_D:
			robot->ScheduleMoveRight();
		break;

		// Sprite tests
		case Qt::Key_T:
			treadmillSprite->Move();
		break;
		case Qt::Key_C:
			checkpointSprite->Reached();
		break;
		case Qt::Key_R:
			repairpointSprite->Reached();
		break;
	}

	NextRound();
}